package com.jiesen.base.modules.login.department.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiesen.base.modules.login.department.model.Department;
import com.jiesen.base.modules.login.department.service.DepartmentService;
import com.jiesen.base.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    //增加一个部门
    @PostMapping("/create")
    public R createDepartment(Department department){
        boolean save = departmentService.save(department);
        if(!save){
            return R.error();
        }
        return R.ok();
    }

    //删除一个部门
    @GetMapping("/delete")
    public R deleteDepartment(@RequestParam("id")Integer id){
        boolean save = departmentService.removeById(id);
        if(!save){
            return R.error();
        }
        return R.ok();
    }

    //修改一个部门
    @PostMapping("/edit")
    public R editDepartment(Department department){
        boolean save = departmentService.updateById(department);
        if(!save){
            return R.error();
        }
        return R.ok();
    }

    //查询一个部门
    @GetMapping("/view")
    public R viewDepartment(@RequestParam("id")Integer id){
        Department department = departmentService.getById(id);
        if(department==null){
            return R.error();
        }
        return R.ok().put("data",department);
    }

    //根据条件和分页查询部门
    @PostMapping("/view")
    public R viewDepartmentBy(Department department){
        return departmentService.page(department);
    }

    //根据父节点id查询部门
    @GetMapping("/viewParent")
    public R viewDepartmentByParent(@RequestParam("parentId")Integer id){
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parnet_id",id);
        List<Department> list = departmentService.list(queryWrapper);
        return R.ok().put("data",list);
    }

    //用户添加至部门
    @PostMapping("/addUser")
    public R addUSer(Integer uid,Integer did){

        departmentService.insertDepartmentUser(uid,did);

        return R.ok();
    }

}
